/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.hycreate.platform.modules.crm.business;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hycreate.platform.common.config.Global;
import com.hycreate.platform.common.utils.StringUtils;
import com.hycreate.platform.common.web.BaseController;
import com.hycreate.platform.modules.crm.project.ProjectService;
import com.hycreate.platform.modules.sys.entity.Office;
import com.hycreate.platform.modules.sys.utils.UserUtils;

/**
 * 项目添加Controller
 * @author zyc
 * @version 2016-12-05
 */
@Controller
@RequestMapping(value = "${adminPath}/crm/business/businessProject")
public class BusinessProjectController extends BaseController {

	@Autowired
	private BusinessProjectService businessProjectService;
	@Autowired
	private HycrmContactService hycrmContactService;
	@Autowired
	private SysDictService sysDictService;
	@Autowired
	private ProjectService projectService;
	
	@ModelAttribute
	public BusinessProject get(@RequestParam(required=false) String id) {
		BusinessProject entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = businessProjectService.get(id);
		}
		if (entity == null){
			entity = new BusinessProject();
		}
		return entity;
	}

	//@RequiresPermissions("business:businessProject:project")
	@RequestMapping(value = "form")
	public String form(BusinessProject businessProject, Model model) {
		HycrmContact hycrmContact = new HycrmContact();
		hycrmContact.setCustomerId(businessProject.getCustomerId());
		//客户联系人
		List<HycrmContact> hycrmContactList = hycrmContactService.findList(hycrmContact);
		//项目状态
		CrmSysDict d = new CrmSysDict();
		d.setType("hycrm_project_status");
		List<CrmSysDict> sysDictList = sysDictService.findList(d);
		//部门
		List<Office> dept = projectService.findAllDept();
		
		model.addAttribute("hycrmContactList", hycrmContactList);
		model.addAttribute("sysDictList", sysDictList);
		model.addAttribute("businessProject", businessProject);
		model.addAttribute("dept", dept);
		return "modules/crm/business/businessProjectForm";
	}
	
	//@RequiresPermissions("business:businessProject:project")
	@RequestMapping(value = "formChange")
	public String formChange(BusinessProject businessProject, Model model) {
		//查询该客户下的所有联系人
		HycrmContact hycrmContact = new HycrmContact();
		hycrmContact.setCustomerId(businessProject.getCustomerId());
		List<HycrmContact> hycrmContactList = hycrmContactService.findList(hycrmContact);
		//查询所有的项目状态
		CrmSysDict d = new CrmSysDict();
		d.setType("hycrm_project_status");
		List<CrmSysDict> sysDictList = sysDictService.findList(d);
		
		model.addAttribute("hycrmContactList", hycrmContactList);
		model.addAttribute("sysDictList", sysDictList);
		model.addAttribute("businessProject", businessProject);
		return "modules/crm/business/businessProjectFormChange";
	}

	//@RequiresPermissions("business:businessProject:project")
	@RequestMapping(value = "save")
	public String save(BusinessProject businessProject, Model model, RedirectAttributes redirectAttributes) {
		if(businessProject.getFollowMan().equals("-1")){
			businessProject.setFollowMan(UserUtils.getUser().getId());
		}
		if (!beanValidator(model, businessProject)){
			return form(businessProject, model);
		}
		businessProjectService.save(businessProject);
		addMessage(redirectAttributes, "保存项目成功");
		return "redirect:"+Global.getAdminPath()+"/crm/findcustomer/findCustomer/form?id="+businessProject.getCustomerId();
	}
	
	//@RequiresPermissions("business:businessProject:project")
	@RequestMapping(value = "delete")
	public String delete(BusinessProject businessProject, RedirectAttributes redirectAttributes) {
		businessProjectService.delete(businessProject);
		addMessage(redirectAttributes, "删除项目成功");
		return "redirect:"+Global.getAdminPath()+"/crm/findcustomer/findCustomer/form?id="+businessProject.getCustomerId();
	}

}